package dota.rules;

import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Area;
import java.util.Vector;

import dota.entity.ArmedUnit;
import dota.entity.ArmedUnitSoldier;
import gameframework.base.IntersectTools;
import gameframework.base.Movable;
import gameframework.base.SpeedVector;
import gameframework.game.MoveBlocker;
import gameframework.game.MoveBlockerCheckerDefaultImpl;

public class DotaMoveBlockerChecker extends MoveBlockerCheckerDefaultImpl {
	

	public DotaMoveBlockerChecker() {
		
	}

	@Override
	public boolean moveValidation(Movable m, SpeedVector mov) {
		Shape intersectShape = IntersectTools.getIntersectShape(m, mov);
		Vector<MoveBlocker> moveBlockersInIntersection = new Vector<MoveBlocker>();
		Area intersectArea = new Area(intersectShape);
		Rectangle tmpIntersec = (intersectShape.getBounds());

		for (MoveBlocker moveBlocker : moveBlockers) {
			Rectangle tmpB = moveBlocker.getBoundingBox();
			if(!m.getBoundingBox().equals(tmpB)) {
				if (tmpIntersec.intersects(tmpB)) {
					Area tmpArea = new Area(tmpB);
					tmpArea.intersect(intersectArea);
					if (!tmpArea.isEmpty()) {
						if(moveBlocker instanceof ArmedUnit) {
							if (((ArmedUnit)moveBlocker).getName().contains("teamBlue") != ((ArmedUnit) m).getName().contains("teamBlue")) {
								moveBlockersInIntersection.add(moveBlocker);
								((ArmedUnitSoldier)m).setBlocker(moveBlocker);
							}
						}
						else
							moveBlockersInIntersection.add(moveBlocker);
					}
				}
			}
		}

		if (!moveBlockersInIntersection.isEmpty()) {
			return moveBlockerRuleApplier.moveValidationProcessing(
					moveBlockersInIntersection, m);
		}

		return true;
	}
}
